/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-20 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.utility;

import java.io.File;
import java.util.Calendar;


public class TemporaryDirectoryFactory {

	private static File rootTemporaryDirectory = null;
	private static TemporaryDirectoryFactory temporaryDirectoryFactory;

	private TemporaryDirectoryFactory() {} 
	
	public static TemporaryDirectoryFactory getInstance() {
		if (temporaryDirectoryFactory == null) 
			temporaryDirectoryFactory = new TemporaryDirectoryFactory(); 
		return temporaryDirectoryFactory;
	}

	public static File getRootTemporaryDirectory() throws TemporaryDirectoryException {
		if (rootTemporaryDirectory == null) { 
			String directory = System.getProperty("user.dir");
			if (directory == null){
				throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_PROPERTY, "System property [user.dir] is null.");
			}
			File file = new File(directory);
			if (!file.isDirectory()){
				throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_PROPERTY, "System property [user.dir]=" + directory + " is not a directory.");
			}
			directory = directory + File.separator + "mizartools" + File.separator + "temp";
			File temporaryDirectory = new File(directory);
			if (!temporaryDirectory.exists()) {
				if (!temporaryDirectory.mkdirs()){
					throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_DIRECTORY, "It is not possible to create the directory [" + directory + "].");
				};
			}
			rootTemporaryDirectory = temporaryDirectory;
		}
		return rootTemporaryDirectory;
	}
	
	public static TemporaryDirectory newTemporaryDirectory() throws TemporaryDirectoryException {
		String rootDirectory = getRootTemporaryDirectory().getPath();
		String directory = null;
		long id = Calendar.getInstance().getTimeInMillis();
		File file = null;
		int securityLoop = 0;
		{
			securityLoop++;
			id++;
			directory = rootDirectory + File.separator + "tmp" + id;
			file = new File(directory);
			if (!file.exists()) {
				if (!file.mkdirs()){
					throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_DIRECTORY, "It is not possible to create the directory [" + directory + "].");
				};
			} else
			{
				file = null;
			}
		}
		while (file == null && securityLoop <= 9999);
		if (file == null)	throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_DIRECTORY, "It is not possible to create the directory [" + directory + "].");
		return new TemporaryDirectory(id, file);
	}

	public static TemporaryDirectory newTemporaryDirectory(long id) throws TemporaryDirectoryException {
		String rootDirectory = getRootTemporaryDirectory().getPath();
		String directory = null;
		File file = null;
		directory = rootDirectory + File.separator + "tmp" + id;
		file = new File(directory);
		if (!file.exists()) {
			if (!file.mkdirs()){
				throw new TemporaryDirectoryException(TemporaryDirectoryException.NOT_VALID_DIRECTORY, "It is not possible to create the directory [" + directory + "].");
			};
		};
		return new TemporaryDirectory(id, file);
	}
	
	public static void clearAllTemporaryDirectories() {
		clearTemporaryDirectories(Long.MAX_VALUE);
	}
	
	public static void clearTemporaryDirectories(long beforeTime) {
		File file = null;
		try {
			file = getRootTemporaryDirectory();
		} catch (TemporaryDirectoryException e) {
			e.printStackTrace();
			return;
		}
		if (file == null) return;
		if (!file.getParent().contains("mizartools")) return;
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++){
			if (files[i].lastModified() <= beforeTime && files[i].getName().startsWith("tmp") ){
				if (files[i].isDirectory()) {
					clearTemporaryDirectory(files[i]);
				}
				files[i].delete();
			}
		}
	}

	private static void clearTemporaryDirectory(File file){
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++){
			if (files[i].isDirectory()) {
				clearTemporaryDirectory(files[i]);
			}
			files[i].delete();
		}
	}

	
}
